/**
 * 
 */
package com.gragra.util.tests;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import com.gragra.util.OpenUnitElementCounter;
import com.gragra.util.OpenUnitElementCounter.MappingIterator;
/**
 * @author Christoph Teichmann
 * created Jul 5, 2013 10:54:34 PM
 * @version 0.1
 */
public class TestOpenUnitElementCounter
{
	/**
	 * 
	 */
	private OpenUnitElementCounter oue;
	/**
	 *
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception
	{
		oue = new OpenUnitElementCounter();
		oue.add(2, 15, 3.0);
		oue.add(2, 15, 4.75);
		oue.add(5, 7, 5.32);
		oue.add(2, 1, 2.0);
	}
	/**
	 * Test method for {@link com.gragra.util.OpenUnitElementCounter#getSum(int)}.
	 */
	@Test
	public final void testGetSum()
	{
		assertEquals(oue.getSum(2),9.75,0.00001);
		assertEquals(oue.getSum(5),5.32,0.00001);
		oue.add(5, 1,-2.1);
		assertEquals(oue.getSum(5),3.22,0.00001);
	}
	/**
	 * Test method for {@link com.gragra.util.OpenUnitElementCounter#clear()}.
	 */
	@Test
	public final void testClear()
	{
		assertEquals(oue.getSum(2),9.75,0.00001);
		assertEquals(oue.getSum(5),5.32,0.00001);
		assertEquals(oue.getValue(2, 1),2.0,0.00001);
		assertEquals(oue.getValue(2, 15),7.75,0.00001);
		oue.add(5, 1,-2.1);
		assertEquals(oue.getSum(5),3.22,0.00001);
		oue.clear();
		assertEquals(oue.getSum(2),0.0,0.00001);
		assertEquals(oue.getSum(5),0.0,0.00001);
		assertEquals(oue.getValue(2, 15),0.0,0.00001);
		oue.add(0, 15, 3.0);
		assertEquals(oue.getValue(0, 15),3.0,0.00001);
	}
	/**
	 * Test method for {@link com.gragra.util.OpenUnitElementCounter#getValue(int, int)}.
	 */
	@Test
	public final void testGetValue()
	{
		assertEquals(oue.getSum(2),9.75,0.00001);
		assertEquals(oue.getSum(5),5.32,0.00001);
		assertEquals(oue.getValue(2, 1),2.0,0.00001);
		assertEquals(oue.getValue(2, 15),7.75,0.00001);
		oue.add(5, 1,-2.1);
		assertEquals(oue.getSum(5),3.22,0.00001);
		oue.clear();
		assertEquals(oue.getSum(2),0.0,0.00001);
		assertEquals(oue.getSum(5),0.0,0.00001);
		assertEquals(oue.getValue(2, 15),0.0,0.00001);
		oue.add(0, 15, 3.0);
		assertEquals(oue.getValue(0, 15),3.0,0.00001);
		assertEquals(oue.getValue(7, 2),0.0,0.00001);
		oue.add(7, 2, 17);
		oue.add(7, 2, 1.22);
		assertEquals(oue.getValue(7, 2),18.22,0.00001);
	}
	/**
	 * Test method for {@link com.gragra.util.OpenUnitElementCounter#set(int, int, double)}.
	 */
	@Test
	public final void testSet()
	{
		assertEquals(oue.getSum(2),9.75,0.00001);
		assertEquals(oue.getSum(5),5.32,0.00001);
		assertEquals(oue.getValue(2, 1),2.0,0.00001);
		assertEquals(oue.getValue(2, 15),7.75,0.00001);
		oue.add(5, 1,-2.1);
		assertEquals(oue.getSum(5),3.22,0.00001);
		oue.clear();
		assertEquals(oue.getSum(2),0.0,0.00001);
		assertEquals(oue.getSum(5),0.0,0.00001);
		assertEquals(oue.getValue(2, 15),0.0,0.00001);
		oue.add(0, 15, 3.0);
		assertEquals(oue.getValue(0, 15),3.0,0.00001);
		assertEquals(oue.getValue(7, 2),0.0,0.00001);
		oue.add(7, 2, 17);
		oue.add(7, 2, 1.22);
		assertEquals(oue.getValue(7, 2),18.22,0.00001);
		oue.set(7, 2, 2.0);
		assertEquals(oue.getSum(7),2.0,0.00001);
	}
	/**
	 * Test method for {@link com.gragra.util.OpenUnitElementCounter#add(com.gragra.util.OpenUnitElementCounter)}.
	 */
	@Test
	public final void testAddOpenUnitElementCounter()
	{
		assertEquals(oue.getSum(2),9.75,0.00001);
		assertEquals(oue.getSum(5),5.32,0.00001);
		assertEquals(oue.getValue(2, 1),2.0,0.00001);
		assertEquals(oue.getValue(2, 15),7.75,0.00001);
		oue.add(5, 1,-2.1);
		assertEquals(oue.getSum(5),3.22,0.00001);
		oue.clear();
		assertEquals(oue.getSum(2),0.0,0.00001);
		assertEquals(oue.getSum(5),0.0,0.00001);
		assertEquals(oue.getValue(2, 15),0.0,0.00001);
		oue.add(0, 15, 3.0);
		assertEquals(oue.getValue(0, 15),3.0,0.00001);
		assertEquals(oue.getValue(7, 2),0.0,0.00001);
		oue.add(7, 2, 17);
		oue.add(7, 2, 1.22);
		assertEquals(oue.getValue(7, 2),18.22,0.00001);
		oue.set(7, 2, 2.0);
		assertEquals(oue.getSum(7),2.0,0.00001);
		OpenUnitElementCounter oue = new OpenUnitElementCounter();
		oue.set(3, 1, 0.25);
		oue.set(7, 2, 11.0);
		oue.set(7, 1, 0.25);
		this.oue.add(oue);
		assertEquals(this.oue.getValue(7, 2),13.0,0.00001);
		assertEquals(this.oue.getValue(7, 1),0.25,0.00001);
		assertEquals(this.oue.getValue(3, 1),0.25,0.00001);
		assertEquals(this.oue.getSum(7),13.25,0.00001);
		assertEquals(this.oue.getSum(3),0.25,0.00001);
		assertEquals(this.oue.getValue(0, 15),3.0,0.00001);
		assertEquals(this.oue.getSum(0),3.0,0.00001);
	}
	/**
	 * Test method for {@link com.gragra.util.OpenUnitElementCounter#subtract(com.gragra.util.OpenUnitElementCounter)}.
	 */
	@Test
	public final void testSubtract()
	{
		assertEquals(oue.getSum(2),9.75,0.00001);
		assertEquals(oue.getSum(5),5.32,0.00001);
		assertEquals(oue.getValue(2, 1),2.0,0.00001);
		assertEquals(oue.getValue(2, 15),7.75,0.00001);
		oue.add(5, 1,-2.1);
		assertEquals(oue.getSum(5),3.22,0.00001);
		oue.clear();
		assertEquals(oue.getSum(2),0.0,0.00001);
		assertEquals(oue.getSum(5),0.0,0.00001);
		assertEquals(oue.getValue(2, 15),0.0,0.00001);
		oue.add(0, 15, 3.0);
		assertEquals(oue.getValue(0, 15),3.0,0.00001);
		assertEquals(oue.getValue(7, 2),0.0,0.00001);
		oue.add(7, 2, 17);
		oue.add(7, 2, 1.22);
		assertEquals(oue.getValue(7, 2),18.22,0.00001);
		oue.set(7, 2, 2.0);
		assertEquals(oue.getSum(7),2.0,0.00001);
		OpenUnitElementCounter oue = new OpenUnitElementCounter();
		oue.set(3, 1, 0.25);
		oue.set(7, 2, 11.0);
		oue.set(7, 1, 0.25);
		this.oue.subtract(oue);
		assertEquals(this.oue.getValue(7, 2),-9.0,0.00001);
		assertEquals(this.oue.getValue(7, 1),-0.25,0.00001);
		assertEquals(this.oue.getValue(3, 1),-0.25,0.00001);
		assertEquals(this.oue.getSum(7),-9.25,0.00001);
		assertEquals(this.oue.getSum(3),-0.25,0.00001);
		assertEquals(this.oue.getValue(0, 15),3.0,0.00001);
		assertEquals(this.oue.getSum(0),3.0,0.00001);
	}
	/**
	 * Test method for {@link com.gragra.util.OpenUnitElementCounter#fastIterator()}.
	 */
	@Test
	public final void testGetIterator()
	{
		MappingIterator mi = oue.getIterator();
		boolean seen = false;
		while(mi.hasNext())
		{
			mi.setToNext();
			if(mi.getCurrentUnit() == 2 && mi.getCurrentElement() == 15)
			{
				seen = true;
				assertEquals(mi.getCurrentValue(),7.75,0.00001);
				assertEquals(mi.getCurrentUnitSum(),9.75,0.00001);
			}
		}
		assertTrue(seen);
	}
}